package com.res.common.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.res.simulator.handler.SelectApiHandler;
import com.res.simulator.model.SelectTemplateModel;
import com.res.simulator.view.ConsumerTemplateSearchResultsView;
import com.res.system.common.SessionRegistry;

@Controller
@SessionAttributes
public class TemplateSelectController extends BaseController {

    @RequestMapping("/populate_popup")
    public String home(@ModelAttribute("SelectTemplateModel") final SelectTemplateModel model, final BindingResult result) {

        return "apipopuplayout";

    }

    /**
     * Controller for main page save button action
     * 
     * @param model
     * @return
     */

    @RequestMapping(value = "/popup_api_selection", method = RequestMethod.GET)
    public String popupTemplateSelectiionDialog(@ModelAttribute("SelectTemplateModel") final SelectTemplateModel model, final BindingResult result) {

        final String apiId = (String) getSession().getAttribute(SessionRegistry.API_ID_HIDDEN_VARIABLE);

        if (null != getSession().getAttribute(SessionRegistry.GENERIC_API_SEARCH_RESULTS_VIEW)) {

            final List<ConsumerTemplateSearchResultsView> genericAPISearchResultsView =
                (List<ConsumerTemplateSearchResultsView>) getSession().getAttribute(SessionRegistry.GENERIC_API_SEARCH_RESULTS_VIEW);

            for (final ConsumerTemplateSearchResultsView consumerTemplateSearchResultsView : genericAPISearchResultsView) {

                if (consumerTemplateSearchResultsView.getApiId().equals(apiId)) {

                    getSession().setAttribute("selectedApiName", consumerTemplateSearchResultsView.getApiName());
                    break;
                }
            }
        }

        return "redirect:populate_popup.htm";
    }

    @RequestMapping(value = "/search_template.htm", method = RequestMethod.GET)
    public String searchTemplates(@ModelAttribute("SelectTemplateModel") final SelectTemplateModel model) throws Exception {

        final String apiId = (String) getSession().getAttribute(SessionRegistry.API_ID_HIDDEN_VARIABLE);
        List<ConsumerTemplateSearchResultsView> searhViewList = new ArrayList<ConsumerTemplateSearchResultsView>();

        if (null == model.getApiId() || (null != model.getApiId() && model.getApiId().trim().length() == 0)) {

            model.setApiId(apiId);

        }

        if (null != model.getApiName()) {

            getSession().setAttribute("selectedApiName", model.getApiName());
        }

        if (null != model
            && ((null != model.getApiName() && model.getApiName().length() != 0) || (null != model.getTemplateId() && model.getTemplateId().trim().length() != 0))) {

            final SelectApiHandler selectapiHandler = new SelectApiHandler();
            searhViewList = selectapiHandler.process(model);

            getSession().setAttribute(SessionRegistry.CONSUMER_TEMPLATE_SELECT_VIEW, searhViewList);
        }

        getSession().setAttribute(SessionRegistry.CONSUMER_TEMPLATE_SELECT_VIEW, searhViewList);

        return "redirect:populate_popup.htm";

    }

    @RequestMapping(value = "/search_template.htm", method = RequestMethod.GET, params = "popupCancel")
    public String closePopup(@ModelAttribute("SelectTemplateModel") final SelectTemplateModel model) throws Exception {

        return "redirect:genericapiexecutionmainpage.htm";
    }

}
